#ifndef _STATIC_LINK_LIST_
#define _STATIC_LINK_LIST_

#include "LinkList.h"

namespace MySTL
{

template < typename T, int N >
class StaticLinkList : public LinkList<T>
{
protected:
	typedef typename LinkList<T>::Node Node;

public:
	StaticLinkList();
	~StaticLinkList();

	int capacity() const;

protected:
	Node* create_node();
	void destory_node(Node* node);

protected:
	char m_space[sizeof(Node) * N];
	bool m_used[N];
};

template < typename T, int N >
StaticLinkList<T, N>::StaticLinkList()
{
	for (int i = 0; i < N; ++i)
	{
		m_used[i] = false;
	}
}

template < typename T, int N >
StaticLinkList<T, N>::~StaticLinkList()
{
	this->clear();
}

template < typename T, int N >
int StaticLinkList<T, N>::capacity() const
{
	return N;
}

template < typename T, int N >
typename StaticLinkList<T, N>::Node* StaticLinkList<T, N>::create_node()
{
	Node* ret = NULL;

	for (int i = 0; i < N; ++i)
	{
		if (false == m_used[i])
		{
			m_used[i] = true;
			ret = reinterpret_cast<Node*>(m_space) + i;
			ret = new(ret) Node();
			break;
		}
	}

	return ret;
}

template < typename T, int N >
void StaticLinkList<T, N>::destory_node(Node* node)
{
	int delta = (int)node - (int)m_space;
	if (delta >= 0 && delta % sizeof(Node) == 0)
	{
		int index = delta / sizeof(Node);
		if (index < N && m_used[index] == true)
		{
			m_used[index] = false;
			node->~Node();
		}
	}
}

}

#endif